package com.woniuxy.test;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @BelongsProject: java-85-course-code
 * @BelongsPackage: com.woniuxy.test
 * @Author: LiTongMiao
 * @Date: 2022/2/28 16:21
 * @Description: 模拟jedis客户端，往Redis服务端发送消息
 */
public class MyjedisClient {
    Socket socket;
    public MyjedisClient(){
        try {
            this.socket = new Socket("localhost",6380);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public MyjedisClient(String url,int port){
        try {
            this.socket = new Socket(url,port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void set(String key,String value) throws IOException {
        StringBuffer sb = new StringBuffer();
        //都是基于日志来模拟对应的数据
        sb.append("*3").append("\r\n");
        sb.append("$4").append("\r\n");
        sb.append("SADD").append("\r\n");
        sb.append("$3").append("\r\n");
        sb.append("set").append("\r\n");
        sb.append("$3").append("\r\n");
        sb.append("$").append(key.length()).append("\r\n");
        sb.append(key).append("\r\n");
        sb.append("$").append(value.length()).append("\r\n");
        sb.append(value).append("\r\n");
        byte[] bytes = sb.toString().getBytes();
         OutputStream outputStream = socket.getOutputStream();
        outputStream.write(bytes);
        socket.close();
    }

}
